package com.ghyf.mplay.bean;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.ghyf.mplay.datatype.AlignMode;
import com.ghyf.mplay.datatype.BackParam;
import com.ghyf.mplay.datatype.FontParam;
import com.ghyf.mplay.datatype.GradientColorType;
import com.ghyf.mplay.datatype.POS;
import com.ghyf.mplay.datatype.RGBA;
import com.ghyf.mplay.datatype.ShowType;
import com.ghyf.mplay.view.MScroll;

/**
 * 显示滚动文字模块的信息包装类
 * @author 陈小红
 *
 */
public class ScrollInfo extends BaseModuleInfo {
//	private static final String TAG="ScrollInfo";
	/*
	 * 文件版本 File_Version CHAR 1 模块名称 Module_Name CHAR Scroll1 UID Module_UID INT
	 * GID Module_GIU INT 模块位置 Mudule_Pos POS - 背景参数 Backgroud BACK_PARAM
	 * 见§3.1.4节 - 对齐方式 Align ENUM 5 左边距 Left_Margin INT 0 上边距 Top_Margin INT 0
	 * 字体 Font FONT_PARAM 见§3.1.2节 宋体40号 缺省文字列表 Text_List - 缺省文字 Content CHAR
	 * 滚动方式 Scroll_Type ENUM 左滚/上滚 0 滚动速度 Scroll_Speed INT 30
	 */
	private Context mContext=null;
	// <Backgroud Type="3" PureColor="(0,0,0,255)" GradientColor1="(0,0,0,255)"
	// GradientColor2="(0,0,0,255)" BKFile="" GradientColorType="0"/>
	private BackParam backgroud=null;

	// <Align>5</Align>
	private AlignMode align;// 对齐方式

	// <Left_Margin>0</Left_Margin>
	private int left_margin;// 左边距

	// <Top_Margin>0</Top_Margin>
	private int top_margin;// 上边距

	// <Scroll_Type>0</Scroll_Type>
	private ShowType scroll_type;// 滚动文本的显示方式。

	// <Scroll_Speed>25</Scroll_Speed>
	private int scroll_speed;

	// <Font Name="宋体" Size="40" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	private FontParam font;

	// <Text_List>
	// <Item Content="欢迎乘坐北京地铁" />
	// <Item Content="Wellcom to Beijing！" />
	// <Item Content="欢迎乘坐北京地铁10号线" />
	// <Item Content="欢迎各位领导莅临指导！" />
	// </Text_List>
	private ArrayList<String> text_list=null;// 待显示的文本列表

	private MScroll mScroll;

	private int refresh_time; // 滚动刷新的时间间隔
	private int scroll_pixel; // 滚动刷新的间距
	private int oneStepMaxSpeed = 20; // 80 当scroll_speed小于该值时每次刷新滚动1个像素，小于该值N倍时滚动N个像素。
	/**是否收到广播 默认false*/
	private boolean isReceive=false;
	
	
	@Override
	public MScroll getView(Context context) {
		mContext=context;
		mScroll = new MScroll(context);
		//设置控件基本属性
		mScroll.setModule_type(module_type);
		mScroll.setzOrder(zOrder);
		mScroll.setFile_version(file_version);
		mScroll.setModule_name(module_name);
		mScroll.setModule_uid(module_uid);
		mScroll.setModule_gid(module_gid);

		
		// 控件位置及大小设置。
		initPosition(mScroll, pos);

		// 背景设置
		initBackground(mScroll, backgroud);

		// 设置字体
		mScroll.setFont(font);

		// 设置文本列表
		mScroll.setText_list(text_list);

		//  对齐方式设置
		
		
		// 设置左、上的padding（用left、top margin变量设置）
		mScroll.setPadding(left_margin, top_margin, 0, 0);

		if (scroll_speed <= 0 || scroll_speed >= 2000) { // 当速度为零或太大时，选默认速度
			scroll_pixel = 2;
			refresh_time = 20;
		} else { // 根据scroll_speed和oneStepMaxSpeed算出合适的步长scroll_pixel，再决定滚动刷新的时间。
//			scroll_pixel = scroll_speed / oneStepMaxSpeed + 1;
//			refresh_time = 1000 * scroll_pixel / scroll_speed;
			
			scroll_pixel = Math.round((float)scroll_speed/oneStepMaxSpeed)  ;
			refresh_time = 10;
			
//			scroll_pixel = scroll_speed  ;
//			refresh_time = 1000;
//			LogUtil.i(TAG, "scroll_pixel("+scroll_pixel+"),refresh_time("+refresh_time+")");
		}

		mScroll.setScroll_pixel(scroll_pixel);
		mScroll.setRefresh_time(refresh_time);

		mScroll.setCurrentScrollX(pos.getWidth());// 设置滚动文本的初始位置。

		return mScroll;

	}

	/**
	 * 初始化控件的背景
	 * 
	 * @param view
	 *            被设置的控件
	 * @param bg
	 *            控件的背景
	 */
	void initBackground(View view, BackParam bg) {
		switch (bg.getType()) {
		case NOTSHOW: // 背景不显示
			break;
		case GRADIENTCOLOR: // 背景类型为渐变色
			if (bg.getColorType() == GradientColorType.HORIZONTALGRADIENT) {
				//水平渐变 参考：url:http://blog.csdn.net/a_large_swan/article/details/7107126
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			} else {
				// 垂直渐变
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			}
			break;
		case PICTURE: // 背景类型为图片
			// 本地图片文件路径生成drawable
			Bitmap bitmap = BitmapFactory.decodeFile(bg.getBkfile().getPath());
			Drawable drawable = new BitmapDrawable(mContext.getResources(),bitmap);
			// 设置图片背景
			view.setBackgroundDrawable(drawable);
			break;
		case PURECOLOR: // 背景类型为纯色
			RGBA purecolor = bg.getPurecolor();
			view.setBackgroundColor(Color.argb(purecolor.getAlpha(),
					purecolor.getRed(), purecolor.getGreen(),
					purecolor.getBlue()));
			break;
		default: // 其他
			break;
		}
	}

	/**
	 * 初始化控件的位置
	 * 
	 * @param view
	 *            被设置的控件
	 * @param pos
	 *            控件的位置
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	void initPosition(View view, POS pos) {
		view.setX(pos.getLeft());
		view.setY(pos.getTop());
		view.setLayoutParams(new LayoutParams(pos.getWidth(), pos.getHeight()));
	}
	/**
	 * 设置当前View显示/隐藏
	 * 2014-5-21 下午3:25:01
	 * @param isShow
	 *
	 */
	public void setVisibility(boolean isShow){
		if(isShow){
			mScroll.setVisibility(View.VISIBLE);
		}else{
			mScroll.setVisibility(View.GONE);
		}
	}
	/**
	 * @return the mScroll
	 */
	public MScroll getmScroll() {
		return mScroll;
	}

	/**
	 * @param mScroll the mScroll to set
	 */
	public void setmScroll(MScroll mScroll) {
		this.mScroll = mScroll;
	}

	public BackParam getBackgroud() {
		return backgroud;
	}

	public void setBackgroud(BackParam backgroud) {
		this.backgroud = backgroud;
	}

	public AlignMode getAlign() {
		return align;
	}

	public void setAlign(AlignMode align) {
		this.align = align;
	}

	public int getLeft_margin() {
		return left_margin;
	}

	public void setLeft_margin(int left_margin) {
		this.left_margin = left_margin;
	}

	public int getTop_margin() {
		return top_margin;
	}

	public void setTop_margin(int top_margin) {
		this.top_margin = top_margin;
	}

	public ShowType getScroll_type() {
		return scroll_type;
	}

	public void setScroll_type(ShowType scroll_type) {
		this.scroll_type = scroll_type;
	}

	public int getScroll_speed() {
		return scroll_speed;
	}

	public void setScroll_speed(int scroll_speed) {
		this.scroll_speed = scroll_speed;
	}

	public FontParam getFont() {
		return font;
	}

	public void setFont(FontParam font) {
		this.font = font;
	}

	public ArrayList<String> getText_list() {
		return text_list;
	}

	public void setText_list(ArrayList<String> text_list) {
		this.text_list = text_list;
	}

	/**
	 * @return the isReceive
	 */
	public boolean isReceive() {
		return isReceive;
	}

	/**
	 * @param isReceive the isReceive to set
	 */
	public void setReceive(boolean isReceive) {
		this.isReceive = isReceive;
	}

	
	

}